//
//  ViewController.m
//  oc1
//
//  Created by kokuma on 2017/2/3.
//  Copyright © 2017年 kokumams. All rights reserved.
//

#import "ViewController.h"
#import "UIButton+Bootstrap.h"
#import "ConversationCell.h"
#import "OpenAccountBean.h"
#import "BaseNavigationController.h"
// #import "TopicController.h"
// #import "VenueController.h"
// #import "AcgGridController.h"
// #import "UserController.h"
// #import "BaseTabBarController.h"
// #import "LoginController.h"
//#import "BDMapController.h"
#import "WebViewController.h"
#import "EditStoreController.h"
#import "SectorMenuController.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong,readwrite) NSMutableArray *dataList;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     [self.view addSubview:self.customView];
    //    添加myTableView
    _myTableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [tableView registerClass:[ConversationCell class] forCellReuseIdentifier:kCellIdentifier_Conversation];

        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [self.view addSubview:tableView];
//        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.view);
//        }];
        tableView;
    });
   // [self.view addSubview:_myTableView];
   //[self testAFNetworking];
}
- (UIImage *)processUsingGPUImage:(UIImage*)input {
    // 1. Create the GPUImagePictures
    GPUImagePicture * inputGPUImage = [[GPUImagePicture alloc] initWithImage:input];
    UIImage * ghostImage = [UIImage imageNamed:@"tab_explore_h"];
    GPUImagePicture * ghostGPUImage = [[GPUImagePicture alloc] initWithImage:ghostImage];
    // 2. Set up the filter chain
    GPUImageAlphaBlendFilter * alphaBlendFilter = [[GPUImageAlphaBlendFilter alloc] init];
    alphaBlendFilter.mix = 0.5;
    [inputGPUImage addTarget:alphaBlendFilter atTextureLocation:0];
    [ghostGPUImage addTarget:alphaBlendFilter atTextureLocation:1];
    GPUImageGrayscaleFilter * grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];
    [alphaBlendFilter addTarget:grayscaleFilter];
    // 3. Process & grab output image
    [grayscaleFilter useNextFrameForImageCapture];
    [inputGPUImage processImage];
    [ghostGPUImage processImage];
    UIImage * output = [grayscaleFilter imageFromCurrentFramebuffer];
    return output;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UIView *)customView{
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
    //button
   UIButton *footerBtn = [UIButton buttonWithStyle:StrapSuccessStyle andTitle:@"点我点我" andFrame:CGRectMake(100, 60, 100, 50) target:self action:@selector(sendActivate)];
    [footerV addSubview:footerBtn];
//    RAC(self, footerBtn.enabled) = [RACSignal combineLatest:@[RACObserve(self, global_key)] reduce:^id(NSString *global_key){
//        return @(global_key.length > 0);
//    }];
    return footerV;
}

- (UILabel *) txtView{
    UILabel *txtView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
    
    return txtView;
}


- (void)sendActivate {
    EditStoreController *venueVC = [EditStoreController new];
    BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:venueVC];
    [self presentViewController:nav animated:true completion:^{
        //
    }];
 //   [self showActionSheet];
}

- (void)showActionSheet{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"选一个呗" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"啥玩意"  otherButtonTitles:@"淘宝",@"百度",nil];
    [sheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
   
       NSString *url;
       if (buttonIndex==1){
          url=@"https://www.taobao.com";
       }else if(buttonIndex==2){
          url=@"https://www.baidu.com/";
       }
    
      if(buttonIndex!=actionSheet.cancelButtonIndex){
        WebViewController *venueVC = [[WebViewController alloc]initWithUrl:url];
        BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:venueVC];
        [self presentViewController:nav animated:true completion:^{
            //
        }];
  
       }
    }

- (void)testAFNetworking {
     printf("testAFNetworking1");
    MPNetAPIManager *netAPI = [MPNetAPIManager sharedManager];
    [netAPI getRecommendUsers:@"1" andBlock:^(id data, NSError *error) {
        if(data){
            NSMutableArray *accounts = data;
            if(!_dataList){
                _dataList = [NSMutableArray new];
            }
            [_dataList removeAllObjects];
            for(id openA in accounts){
               OpenAccountBean *bean = [[OpenAccountBean alloc] initWithDictionary:openA];
                NSLog(bean.displayName);
                NSLog([@(bean.id.longValue) stringValue]);
                [_dataList addObject:bean];
            }
            [_myTableView reloadData];
 //           id  openA = accounts[0][@"id"];
           // OpenAccountBean *bean = data[0];
           // NSLog(bean.displayName);
           
//            NSNumber *openA2 = openA;
//            long value= openA2.longValue ;
//            NSLog([@(value) stringValue]);
        }
       
        
        
    }];
  }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(_dataList){
        return _dataList.count;
    }else{
        return 100;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ConversationCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Conversation forIndexPath:indexPath];
    if(_dataList){
        cell.bean = _dataList[indexPath.row];
    }
   // __weak typeof(self) weakSelf = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ConversationCell cellHeight];
}
@end
